package com.huweilong.tingshu.servlet.publish;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.huweilong.tingshu.repository.AlbumRepo;
import com.huweilong.tingshu.util.Log;
import com.huweilong.tingshu.view_object.publish.ResultView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 是阿秋啊
 * @date 2022/07/23 09:59
 **/
@WebServlet("/album-list.json")
public class AlbumListServlet extends HttpServlet {
    private final AlbumRepo albumRepo = new AlbumRepo();
    private final ResultView resultView = new ResultView();
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resultView.albumViewList = albumRepo.selectAlbumsAllPublish();

        String json = objectMapper.writeValueAsString(resultView);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        resp.getWriter().println(json);
        Log.println("JSON写入响应体" + json);
    }
}